package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.admin.StorageDeleteBO;
import com.wangdao.wechatmall.bean.bo.admin.StorageListBO;
import com.wangdao.wechatmall.bean.bo.admin.StorageUpdateBo;
import com.wangdao.wechatmall.bean.pojo.Storage;
import com.wangdao.wechatmall.bean.pojo.StorageExample;
import com.wangdao.wechatmall.bean.vo.admin.AdminDataVO;
import com.wangdao.wechatmall.bean.vo.admin.StorageListItemVO;
import com.wangdao.wechatmall.bean.vo.admin.StorageUpdateVO;
import com.wangdao.wechatmall.mapper.StorageMapper;
import com.wangdao.wechatmall.util.DateTransformUtil;
import com.wangdao.wechatmall.util.BooleanTransformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: StorageServiceImpl
 * @Description ${DESCRIPTION}
 * @Author Xinhao Yi
 * @Date 2021/7/11  2:47
 */
@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    StorageMapper storageMapper;

    @Override
    public AdminDataVO<StorageListItemVO> queryStorage(StorageListBO storageListBO) {
        PageHelper.startPage(storageListBO.getPage(), storageListBO.getLimit());

        StorageExample storageExample = new StorageExample();
        StorageExample.Criteria criteria = storageExample.createCriteria();
        if (storageListBO.getName() != null) {
            criteria.andNameLike("%" + storageListBO.getName() + "%");
        }
        if (storageListBO.getKey() != null) {
            criteria.andKeyEqualTo(storageListBO.getKey());
        }
        //只拿没被删掉的，即deleted为0的

        //拿时间
        //当前时间戳
        Date date = new Date(System.currentTimeMillis());
        Date formattedDate = DateTransformUtil.dateFormat(date);

        storageExample.setOrderByClause(storageListBO.getSort() + " " + storageListBO.getOrder());
        List<Storage> storages = storageMapper.selectByExample(storageExample);

        ArrayList<StorageListItemVO> storageListItemVOS = new ArrayList<>();
        for (Storage storage : storages) {
            //只拿没被删除的
            if (storage.getDeleted() == 0) {
                Boolean deleted = BooleanTransformUtils.deletedInteger2Boolean(storage.getDeleted());
                StorageListItemVO storageListItemVO = new StorageListItemVO(storage.getId(), storage.getKey(), storage.getName(), storage.getType(),
                        storage.getSize(), storage.getUrl(), formattedDate, formattedDate, deleted);
                //加入到返回list中
                storageListItemVOS.add(storageListItemVO);
            }
        }
        PageInfo<Storage> storagePageInfo = new PageInfo<>(storages);
        int total = (int) storagePageInfo.getTotal();
        return AdminDataVO.ok(total,storageListItemVOS);
    }

    @Override
    public StorageUpdateVO changeStorage(StorageUpdateBo param) {
        //更新时间
        Date date = new Date(System.currentTimeMillis());
        Date updateTime = DateTransformUtil.dateFormat(date);
        Storage storage = new Storage();
        storage.setId(param.getId());
        storage.setName(param.getName());
        storage.setUpdateTime(DateTransformUtil.dateStringFormat(date));

        //更新数据库
        storageMapper.updateByPrimaryKeySelective(storage);

        //构造返回VO
        StorageUpdateVO storageUpdateVO = new StorageUpdateVO(param.getId(), param.getKey(), param.getName(), param.getType(),
                param.getSize(), param.getUrl(), param.getAddTime(), updateTime, param.isDeleted());
        return storageUpdateVO;
    }

    //删掉静态资源
    @Override
    public void removeStorage(StorageDeleteBO storageDeleteBO) {
        Storage storage = new Storage();
        storage.setId(storageDeleteBO.getId());
        storage.setDeleted(1);
        //更新数据库
        storageMapper.updateByPrimaryKeySelective(storage);
    }
}
